﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Rodunu.Business.Interfaces;
using Rodunu.Business.Models.User;

namespace Rodunu.Clients.WPF
{
	/// <summary>
	/// Interaction logic for SearchResults.xaml
	/// </summary>
	public partial class SearchResults
	{
	    private readonly ICollection _myCollection;
	    private readonly IManageSettings _manageSettings;
	    private double _height;
	    private double _width;
        private int _numberOfControls = -1;
        private readonly BackgroundWorker _backgroundWorker;
	    private int _columns = -1;

		public SearchResults(ICollection myCollection, IManageSettings manageSettings)
		{
		    _myCollection = myCollection;
		    _manageSettings = manageSettings;

			InitializeComponent();

            ParentContainer.SizeChanged += ParentContainer_SizeChanged;

		    _backgroundWorker = new BackgroundWorker();
            _backgroundWorker.DoWork += _backgroundWorker_DoWork;
            _backgroundWorker.RunWorkerCompleted += _backgroundWorker_RunWorkerCompleted;
        }

        void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            SetHeight();
        }

        void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            _width = ((Size)e.Argument).Width - 250;

            if (_numberOfControls == -1)
            {
                if (_manageSettings.CurrentUser != null && _manageSettings.CurrentUser.Accounts != null)
                {
                    _numberOfControls = ((from acct in _manageSettings.CurrentUser.Accounts
                                          where acct.IsEnabled
                                          select acct).ToList()).Count;
                }
            }
            
            if (_columns == -1)
            {
                if (_numberOfControls > 1)
                    _columns = Convert.ToInt32(_width / 250) - 1;
                else
                    _columns = 0;
            }

            if (_numberOfControls > _columns)
            {
                int rows = _numberOfControls / _columns + 1;
                _height = (((Size)e.Argument).Height / rows) - 20;
            }
            else
                _height = ((Size)e.Argument).Height - 20;

        }

        void ParentContainer_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            _backgroundWorker.RunWorkerAsync(e.NewSize);
        }

        private void SetHeight()
        {
            foreach (var o in SearchItemSources.Children)
            {
                ((ISearchControl)o).SetHeight(_height);
            }
        }

        public void Search(string searchFor)
        {
            SearchFor.Content = searchFor;

            SearchItemSources.Children.Clear();
            SearchCollectionFriends.Children.Clear();
            
            var collectionSearchResults = new CollectionSearchResults(searchFor, _myCollection);
            SearchCollectionFriends.Children.Add(collectionSearchResults);

            var friendsSearchResults = new FriendsSearch(searchFor);
            SearchCollectionFriends.Children.Add(friendsSearchResults);

            if (_manageSettings.CurrentUser != null && _manageSettings.CurrentUser.Accounts != null)
            {
                foreach (var account in _manageSettings.CurrentUser.Accounts)
                {
                    if (account.IsEnabled)
                    {
                        switch (account.AccountType)
                        {
                            case AccountTypes.Amazon:
                                var amazonSearch = new AmazonSearch(searchFor, _height);
                                SearchItemSources.Children.Add(amazonSearch);
                                break;
                            case AccountTypes.Fandango:
                                break;
                            case AccountTypes.IMDB:
                                var imdbSearch = new IMDBSearch(searchFor, _height);
                                SearchItemSources.Children.Add(imdbSearch);
                                break;
                            case AccountTypes.MoviesDotCom:
                                break;
                            case AccountTypes.Netflix:
                                var netflixSearch = new NetflixSearch(searchFor, account, _height);
                                SearchItemSources.Children.Add(netflixSearch);
                                break;
                            case AccountTypes.RottenTomatoes:
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }
	}
}